package com.isky.visual.db.resolver;

import com.isky.visual.constant.CommonConstant;
import com.isky.visual.db.enums.DataStoreDimensionEnums;
import com.isky.visual.db.enums.DataStoreTypeEnum;
import com.isky.visual.db.resolver.annotation.HandlerDataStoreResolver;
import com.isky.visual.db.vo.BaseValues;
import com.isky.visual.db.vo.DefaultData;
import com.isky.visual.db.vo.DefaultValues;
import com.isky.visual.db.vo.ReportModel;
import com.isky.visual.util.DBUtil;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO 默认数据集解析器
 * @author wenjs
 */
@Component
@HandlerDataStoreResolver(0)
public class DefaultDataSourceDimensionResolver extends BaseDataSourceDimensionResolver {
    protected Log log = LogFactory.getLog(this.getClass());
    @Override
    public ReportModel buildDataStoreDimension(List<Map<String, Object>> selectMaps, Map<String, String> dimensionsMap) {
        ReportModel rm = new ReportModel();
        if(selectMaps == null || dimensionsMap== null){
            return rm;
        }

        String key1 = dimensionsMap.get(DataStoreDimensionEnums.KEY1.getColumnType());
        String key2 = dimensionsMap.get(DataStoreDimensionEnums.KEY2.getColumnType());
        String key3 = dimensionsMap.get(DataStoreDimensionEnums.KEY3.getColumnType());

        List<BaseValues> listValue = new ArrayList<>();
        List<String> listName = new ArrayList<>();
        try {
            //拿到所有维度数据
            for(Map<String, Object> map :selectMaps){
                String name = String.valueOf(map.get(key2));
                if (!listName.contains(name)) {
                    listName.add(name);
                }
            }

            Map<Object, List<Map<String, Object>>> companyNameSet = selectMaps.stream().collect(Collectors.groupingBy(k -> k.get(key1)));
            Set<Object> objects = companyNameSet.keySet();
            for (Object keyname : objects) {
                List<Map<String, Object>> maps = companyNameSet.get(keyname);

                DefaultValues vs = new DefaultValues();
                Map<String,DefaultData> collectName = new HashMap<>();

                // 拿到当前统计项 包含的维度信息
                for(Map<String, Object> map :maps){
                    DefaultData defaultData = new DefaultData();
                    try {
                        String name = String.valueOf(map.get(key2));
                        defaultData.setName(name);
                        defaultData.setValue(getIntegerDefault(key3, map,0));
                        collectName.put(name,defaultData);
                    }catch (Exception e){
                        log.error("数据集结果解析：{}",e);
                    }
                }

                //补全缺失的维度信息。
                List<DefaultData> finalData = new ArrayList<>();
                for(String name :listName){
                   if(collectName.containsKey(name)){
                       finalData.add(collectName.get(name));
                   }else{
                       DefaultData defaultData = new DefaultData();
                       defaultData.setName(name);
                       defaultData.setValue(0);
                       finalData.add(defaultData);
                   }
                }

                vs.setData(finalData);
                vs.setName(String.valueOf(keyname));
                listValue.add(vs);
            }

            rm.setNames(listName);
            rm.setValues(listValue);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return rm;
    }
}
